<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/1 0001
 * Time: 21:39
 */

namespace module;

use think\Db;
class MessageApi
{
    /**
     * 消息列表
     * @param int $uid 用戶id
     * @param string $type 消息类型  1系统消息  2通知消息
     * @return array
     */
    public  static  function  getMessagelist($uid,$type){

        $list=[];
        $list=Db::table('message_notice')
            ->field('mn_id,mn_type,mn_title,mn_img,mn_content,mn_status,created_at')
            ->where('ui_id',$uid)
           // ->where('mn_type',$type)
            ->where('delete_ts',0)
            ->select();
        if(!$list)   return $list;
        foreach ($list as $k=>$v){
            $list[$k]['mn_content']=mb_substr($v['mn_content'],0,20,'utf-8').'.....';
        }
        return $list;

    }
    /**
     * 消息详情
     * @param int $uid 用戶id
     * @return array
     */
    public static function getMessageInfo($mnId){

       $info=[];
       $info= Db::table('message_notice')->field('mn_id,mn_type,mn_title,mn_img,mn_content,mn_status')->where('mn_id',$mnId)->where('delete_ts',0)->find();
       return $info;
    }

    /**
     * 更新已读
     * @param int $uid 用戶id
     *  @param int $uid 用戶id
     * @return array
     */
    public static function readMessage($uid,$ids){
        $sql = "update message_notice 
            set mn_status = 2,updated_at =:mn_updated_at 
            where ui_id = :uid and mn_id in ({$ids}) and delete_ts = 0";
        return Db::execute($sql,['uid' => $uid,'mn_updated_at' => date('Y-m-d H:i:s') ]);

    }
    /**
     * 删除消息
     *  @param  array $where
     * @return array
     */
    public static function deleteMess($where){
        return Db::table('message_notice')->where($where)->update(['delete_ts'=>time()]);

    }


}